package com.sp.app.service.impl;

import com.sp.app.dao.CourseMapper;
import com.sp.app.dao.UserMapper;
import com.sp.app.domain.Course;
import com.sp.app.domain.User;
import com.sp.app.service.CourseService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service("CourseService")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return courseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Course record) {
        return courseMapper.insert(record);
    }

    @Override
    public Course selectByPrimaryKey(String id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Course record) {
        return courseMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Course> queryAll(Course course) {
        return courseMapper.queryAll(course);
    }

    @Override
    public void export(String[] titles, ServletOutputStream out, User user) {
        try {
            // 第一步，创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("sheet1");
            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow hssfRow = hssfSheet.createRow(0);
            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
            // 居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCell hssfCell = null;
            for (int i = 0; i < titles.length; i++) {
                hssfCell = hssfRow.createCell(i);// 列索引从0开始
                hssfCell.setCellValue(titles[i]);// 列名1
                hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
            }

            // 第五步，写入实体数据
            List<Map<String, String>> mapList = userMapper.download();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (mapList != null && !mapList.isEmpty()) {
                for (int i = 0; i < mapList.size(); i++) {
                    hssfRow = hssfSheet.createRow(i + 1);
                    Map<String,String> dataMap= mapList.get(i);

                    // 第六步，创建单元格，并设置值

                    hssfRow.createCell(0).setCellValue(dataMap.get("mainid"));
                    hssfRow.createCell(1).setCellValue(dataMap.get("id"));
                    hssfRow.createCell(2).setCellValue(dataMap.get("name"));
                    hssfRow.createCell(3).setCellValue(dataMap.get("couserid"));
                    hssfRow.createCell(4).setCellValue(dataMap.get("coursename"));
                    hssfRow.createCell(5).setCellValue("");
                }
            }

            // 第七步，将文件输出到客户端浏览器
            try {
                workbook.write(out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw new Exception("导出信息失败！");
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    }

